package cn.nextProject.service.impl;

import cn.nextProject.entity.User;
import cn.nextProject.mapper.UserMapper;
import cn.nextProject.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Cacheable(cacheNames = "users",
            key = "(#user.username != null ? #user.username : '') + '_' + " +
                    "(#user.realname != null ? #user.realname : '') + '_' + " +
                    "(#user.idCard != null ? #user.idCard : '') + '_' + " +
                    "(#user.phone != null ? #user.phone : '') + '_' + " +
                    "(#user.state != null ? #user.state : '') + '_' + " +
                    "#pageNum + '_' + #pageSize"
            )
    public PageInfo<User> findByUser(User user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.findByUser(user);
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        return userPageInfo;
    }

    /**
     * 查询用户的所有权限
     * @param username
     * @return
     */
    @Cacheable(cacheNames = "user", key = "#username")
    @Override
    public User getUserByUsername(String username) {

        if (ObjectUtils.isEmpty(username)){
            return null;
        }

        User user = userMapper.findByUsername(username);

        return user;
    }

    @Cacheable(cacheNames = "users", key = "#username")
    @Override
    public User getUserByUsernameNoPassword(String username) {
        if (ObjectUtils.isEmpty(username)){
            return null;
        }

        User user = userMapper.getUserByUsernameNoPassword(username);

        return user;
    }

    //示例
//    @Caching(
//            put = @CachePut(cacheNames = "users", key = "#user.id"),
//            evict = @CacheEvict(cacheNames = "user_list", allEntries = true)
//    )
//    public User updateUser(User user) {
//        // 更新用户后，同步更新单个用户缓存，并清空用户列表缓存
//        return userRepository.save(user);
//    }


    /**
     * 保存用户（需要删除缓存）
     * @param user
     */
    @CacheEvict(cacheNames = {"users","user","userList"},allEntries = true,beforeInvocation = true)
    @Override
    public void save(User user) {
        userMapper.save(user);
    }

    @Cacheable(cacheNames = "userHasPass", key = "#username")
    @Override
    public User getUserByUsernameHasPass(String username) {
        if (ObjectUtils.isEmpty(username)){
            return null;
        }

        User user = userMapper.getUserByUsername(username);

        return user;
    }

    @CacheEvict(cacheNames = {"users","user","userList"},allEntries = true,beforeInvocation = true)
    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Cacheable(cacheNames = "userList", key = "(#roleGroupId != null ? #roleGroupId : '')")
    @Override
    public List<User> findAllNoRoleGroup(Long roleGroupId) {
        List<User> allNoRoleGroup = new ArrayList<>();
        if (!ObjectUtils.isEmpty(roleGroupId)){
            allNoRoleGroup = userMapper.findAllNoRoleGroup(roleGroupId);
        }
        return allNoRoleGroup;
    }


}
